# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'page5.ui'
#
# Created: Wed Sep 23 11:08:35 2009
#      by: PyQt4 UI code generator 4.4.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui
from PythonUI.Ui_Page5 import Ui_Page5
import os
import sys
import re
from Main import *
from gestion import *
from DAC.CampagneDAC import Campagne

class Page5(Ui_Page5,QtGui.QDialog):
    """
    Page de selection des mesures a effectuer
    """
    def __init__(self,session):
        self.app = QtGui.QApplication(sys.argv)
        QtGui.QDialog.__init__(self)
        self.session=session
        self.parametre={}
        self.config={}
        self.step=50
        self.retour=4
        self.next=7
        self.env=0
        self.radio=1
        self.stattrafic=0
        self.manuel=0
        self.automatique=0
        self.parametre ={}

    def recup_dico(self,filename):
        """
        Construction du dictionnaire depuis un fichier local
        """
        fichier=open(os.path.expanduser('~') + '/.GWA/' +filename,'r')
        expr=re.compile("[A-Za-z]")
        for line in fichier.readlines():
            if expr.findall(line):
                self.parametre[line.split(":")[0].split("\n")[0]]=line.split(":")[1].split("\n")[0]
        fichier.close()

    def suivant(self):
        """
        Fonction appele lors de la validation du bouton 'Ok'
        """
        self.recup_dico("dicosalle")
        idsalle = self.parametre["id"]
        self.recup_dico("dicoaccesspoint")
        idAP = self.parametre["id"]
        self.dicoCampagne["nom"] =  self.lineEdit_nom.displayText()
        self.dicoCampagne["statradio"] =  self.checkBox_etude_radio.isChecked()
        self.dicoCampagne["stattrafic"] =  self.checkBox_etude_trafic.isChecked()
        self.dicoCampagne["env"] =   self.checkBox_env_perturbe.isChecked()
        self.dicoCampagne["automatique"] =  self.radioButton_automatique.isChecked()
        self.dicoCampagne["manuel"] =  self.radioButton_mauel.isChecked()
        self.dicoCampagne["step"] =  self.spinBox_pas.value()
        camp = Campagne()
        camp.createByDico(self.dicoCampagne)
        camp.insertToDb(idsalle,idAP)       
        file=open(os.path.expanduser('~') + '/.GWA/dicocampagne','a') 
        file.write("\nnom:"+self.lineEdit_nom.displayText())
        file.write("\nstep:"+str(self.step))
        file.write("\nstatradio:"+str(self.radio))
        file.write("\nstattrafic:"+str(self.stattrafic))
        file.write("\nenv:"+str(self.env))
        file.write("\nautomatique:"+str(self.automatique))
        file.write("\nmanuel:"+str(self.manuel))
        file.write("\nid:"+str(camp.id["value"]))
        file.close()
        self.close()
        obj6=Main()
        if self.next == 6 :
            obj6.func6(self.config)
        if self.next == 7 :
            obj6.func7(self.config)
    
       #gestion.iwc(dic)
    
    def iwlist(self):
        """
        Environnement radio perturbe
        """
        coordonee={}
        self.env=1
        #coordonee["x"]=0
        #coordonee["y"]=0
        #gestion1=Gestion("un")
        #gestion1.inserbddiwli(coordonee)  
        #file=open("dicocampagne","a")
        # file.write("\nenv:1")
        # file.close()
       
    def affich(self,param):
        """
        Affichage de la fenetre
        """
        #self.parametre=param
        self.setupUi(self)
        self.show()
        sys.exit(self.app.exec_())

    def pas(self):
        """
        Recuperation du pas
        """
        #file=open("dicocampagne","a")
        #file.write("\nstep:"+str(int(self.spinBox_pas.value())))
        #file.close()
        self.step=int(self.spinBox_pas.value())

    def traffic(self):
        """
        Utilisation de la mesure de traffic
        """
        self.next = 6
   #    file=open("dicocampagne","a")
   #    file.write("\nstattrafic:1")
   #    file.close()
        self.stattrafic=1

    def rad(self):
        """
        Utilisation de la mesure radio
        """
        self.radio=1
     #  file=open("dicocampagne","a")
      # file.write("\nradio:1")
       #file.close()

    def manu(self):
        """
        Selection manuelle du pas
        """
        self.manuel=1

    def auto(self):
        """
        Selection automatique du pas
        """
        self.automatique=1

    def precedent(self):
        """
        Fonction appele lors de la validation du bouton 'Annuler'
        """
        self.close()
        commande="./Main.py "+str(self.retour)
        os.system(commande)

    def setupUi(self, Dialog):
        """
        Definition de l'interface graphique.
        La majorite de cette fonction est decrite dans le fichier genere par pyuic4 dans PythonUI/...
        """
        os.system("touch ~/.GWA/dicocampagne")  
        self.dicoCampagne = {}
        Ui_Page5.setupUi(self,Dialog)
        self.connect(self.checkBox_etude_trafic, QtCore.SIGNAL("clicked()"),self.traffic)
        self.connect(self.radioButton_automatique, QtCore.SIGNAL("clicked()"), self.auto)
        self.connect(self.radioButton_mauel, QtCore.SIGNAL("clicked()"), self.manu)
        self.connect(self.checkBox_etude_radio, QtCore.SIGNAL("clicked()"),self.rad)
        self.connect(self.checkBox_env_perturbe, QtCore.SIGNAL("clicked()"), self.iwlist)    
        self.connect(self.spinBox_pas, QtCore.SIGNAL("valueChanged(int)"), self.pas)
        self.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), self.suivant)
        self.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), self.precedent)
        QtCore.QMetaObject.connectSlotsByName(Dialog)
